import java.util.TreeSet;

public class SolutionInterview2_1 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeDuplicateNodes(ListNode head) {
        TreeSet<Integer> set=new TreeSet<>();
        ListNode first=new ListNode(-1);
        ListNode cur=first;
        while (head!=null){
            if (!set.contains(head.val)){
                set.add(head.val);
                cur.next=new ListNode(head.val);
                cur=cur.next;
            }
            head=head.next;
        }
        return first.next;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionInterview2_1().removeDuplicateNodes(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(3,new ListNode(2,new ListNode(1))))))).next.val);
    }
}
